package com.szqhyxj.youxiangju.biz.creditReport;

import com.szqhyxj.youxiangju.biz.card.CardModel;
import com.szqhyxj.youxiangju.sys.BaseResult;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by leung on 2018/11/4.
 */

public class CreditReportPresenter implements CreditReportContract.Presenter {
    CreditReportContract.View view;
    CreditReportEntity creditReportEntity;
    private int STARTREPORT = 1;
    private int REPORTING = 2;
    private int FINISHREPORT = 3;
    private int FAILREPORT = 4;
    private Disposable disposable;

    public CreditReportPresenter(CreditReportContract.View view) {
        this.view = view;
        creditReportEntity = new CreditReportEntity();
    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void init() {
        view.showCommit(STARTREPORT);

    }

    @Override
    public void getCardInfo(final String cardId) {
        CardModel.getcreditcard(cardId)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<CardModel.Card>>() {
                    @Override
                    public void accept(BaseResult<CardModel.Card> cardBaseResult) throws Exception {
                        if (cardBaseResult.businessLogicSuccess()) {
                            view.showCard(cardBaseResult.getData().getBankCardPic());
                            creditReportEntity.setBankCardNo(cardBaseResult.getData().getBankCardNo());
                            creditReportEntity.setBankCardPic(cardBaseResult.getData().getBankCardPic());
                            creditReportEntity.setCardId(cardBaseResult.getData().getCardId());
                            view.showInfo(creditReportEntity);
                        } else {
                            view.showToast(cardBaseResult.getMessage());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.showToast(throwable.getMessage());
                    }
                });
    }

    @Override
    public void commit() {

    }

    @Override
    public void setFirstprocess() {

    }

    @Override
    public void setSecondprocess() {

    }

    @Override
    public void setThirdprocess() {

    }

    @Override
    public void setFourthprocess() {

    }

    @Override
    public void setshowconsumption() {
        view.showconsumption();
    }

    @Override
    public void setenshowconsumption() {
        view.showenconsumption();
    }

    @Override
    public void setReportFinsh() {
        view.showCommit(FINISHREPORT);
    }

    @Override
    public void setReportFail() {
        view.showCommit(FAILREPORT);
    }

    @Override
    public void commit(CreditReportEntity creditReportEntity) {
      //  view.showenconsumption();
        view.showLoading();
        CreditReportModel.getReport(creditReportEntity)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult>() {
                    @Override
                    public void accept(BaseResult baseResult) throws Exception {
                         view.dismissLoading();
                        if (baseResult.businessLogicSuccess()) {
                            view.showCommit(REPORTING);
                        } else {
                            view.showreportFail();
                            view.showToast(baseResult.getMessage());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.dismissLoading();
                        view.showToast(throwable.getMessage());
                        view.showreportFail();
                    }
                });
    }

    @Override
    public void setTime() {
        startCountDown();
    }


    private ArrayList<Disposable> disposables = new ArrayList<>();

    private static final int MINUTE = 200;

    private void startCountDown() {
        disposable = Observable
                .interval(0, 1, TimeUnit.SECONDS)
                .take(MINUTE)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) {
                        if (aLong == MINUTE - 1) {
                            view.showOut();
                            view.showCommit(FAILREPORT);
                            view.showTime((int) (MINUTE - aLong - 1) + "");
                        } else {
                            view.showTime((int) (MINUTE - aLong - 1) + "");
                        }
                    }
                });
        disposables.add(disposable);
    }


    @Override
    public void clean() {
        if (disposable != null) {
            disposable.dispose();
        }
    }

    @Override
    public void setStart() {

    }
}
